package com.yuecai.modules.yc_version;

import com.yuecai.modules.yc_version.domain.YcVersion;
import com.yuecai.modules.yc_version.service.YcVersionService;
import com.yuecai.modules.yc_version.service.dto.YcVersionQueryCriteria;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController("/api/version")
public class VersionController {


    @Autowired
    private YcVersionService ycVersionService;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping
    public ResponseEntity<Object> updateVersion(@RequestBody  YcVersion map){
        String platfrom = map.getPlatfrom();
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionCode",map.getVersionCode());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "downloadUrl",map.getDownloadUrl());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionInfo",map.getVersionInfo());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "forceUpdate",map.getForceUpdate());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionName",map.getVersionName());
        ycVersionService.create(map);
        return ResponseEntity.ok("成功");
    }


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, YcVersionQueryCriteria criteria) throws IOException {
        ycVersionService.download(ycVersionService.queryAll(criteria), response);
    }

    @PutMapping
    @ApiOperation("修改yc_version")
    public ResponseEntity<Object> update(@RequestBody YcVersion map){
        String platfrom = map.getPlatfrom();
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionCode",map.getVersionCode());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "downloadUrl",map.getDownloadUrl());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionInfo",map.getVersionInfo());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "forceUpdate",map.getForceUpdate());
        redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionName",map.getVersionName());
        ycVersionService.update(map);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping
    @ApiOperation("查询yc_version")
    public ResponseEntity<Object> query(YcVersionQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ycVersionService.queryAll(criteria,pageable), HttpStatus.OK);
    }


    @ApiOperation("删除yc_version")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        ycVersionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
